package com.atlassian.jira.plugin.reports;

import com.atlassian.jira.plugin.report.impl.AbstractReport;

import java.io.IOException;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.servlet.ServletUtilities;

/**
 * This is an abstract Report class that has a utility method that will
 * allow an extending class to easily save a JFreeChart for viewing
 * within an html page.
 */
public abstract class AbstractJFreeChartReport extends AbstractReport {

    /**
     * Provided a JFreeChart this will save the chart in a way that the provided
     * jfreechart viewing servlet can serve the generated content.
     *
     * @param jfc this is the JFreeChart to be saved.
     * @param width is the width of the generated JPEG image.
     * @param height is the height of the generated JPEG image.
     * @return the filename of the generated chart JPEG.
     * @exception IOException is thrown if JFreeChart has a problem saving the file.
     */
    protected String saveChartForViewing(JFreeChart jfc, int width, int height) 
        throws IOException {
        // NOTE: the chart will be deleted directly after it is streamed once.
        String fileName = ServletUtilities
            .saveChartAsJPEG(jfc, width, height, null);
        return fileName;
    }

}